package com.bajiao.app.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

@EnableAsync
@Configuration
public class TaskPoolConfig {

    /**
     * @Description
     * 自定义线程池
     * @date 2024/6/13 11:22
     * @author FanJq
    */
    @Bean("taskExecutor")
    public Executor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //@create by FanJq 2024/6/13 11:24 核心线程池大小
        executor.setCorePoolSize(10);
        //@create by FanJq 2024/6/13 11:24 最大线程数
        executor.setMaxPoolSize(20);
        //@create by FanJq 2024/6/13 11:24 队列中最大的数目
        executor.setQueueCapacity(200);
        //@create by FanJq 2024/6/13 11:24 线程空闲后的最大存活时间
        executor.setKeepAliveSeconds(60);
        //@create by FanJq 2024/6/13 11:24 线程名字前缀
        executor.setThreadNamePrefix("custom-task-");
        //设置此执行程序应该在关闭时阻止的最大秒数，以便在容器的其余部分继续关闭之前等待剩余的任务完成他们的执行
        executor.setAwaitTerminationSeconds(60);
        //等待所有的任务结束后再关闭线程池
        executor.setWaitForTasksToCompleteOnShutdown(true);
        return executor;
    }
}
